package com.jiyang.jyandroid.test02;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jiyang.jyandroid.BaseActivity;
import com.jiyang.jyandroid.R;

import java.lang.ref.WeakReference;

/**
 * Created by jy on 2016/11/4.
 * 自动播放的图片
 */

public class Two_1Activity extends BaseActivity {
    private Button btnPlay;
    private ImageView imageView;
    private int[] imagesId;
    private boolean isPlay = false;
    private Thread thread;
    private MyHandler myHandler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test02_4);
        btnPlay = (Button) findViewById(R.id.btn_play);
        imageView = (ImageView) findViewById(R.id.imageView);
        imagesId = new int[]{R.drawable.image04, R.drawable.image03, R.drawable.image02, R.drawable.image01};
        myHandler = new MyHandler(this);
        btnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isPlay) {
                    isPlay = false;
                    btnPlay.setText("Start Play");
                } else {
                    isPlay = true;
                    btnPlay.setText("Stop Play");
                    if (thread == null) {
                        thread = new Thread(new Runnable() {
                            @Override
                            public void run() {
                                int count = 0;
                                while (isPlay) {
                                    if (count == imagesId.length) {
                                        count = 0;
                                    }
                                    Message message = Message.obtain();
                                    message.what = 1;
                                    message.obj = count;
                                    myHandler.sendMessage(message);
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    count++;
                                }
                            }
                        });
                        thread.start();
                    }
                }
            }
        });
    }

    private static class MyHandler extends Handler {
        WeakReference<Two_1Activity> weakReference;
        Two_1Activity activity;

        MyHandler(Two_1Activity activity) {
            this.weakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            this.activity = weakReference.get();
            if (activity == null) {
                return;
            }
            if (msg.what == 1) {
                activity.imageView.setImageResource(activity.imagesId[(int) msg.obj]);
            }
        }
    }
}
